;;; init-org-publish.el --- Configuration for publish org file -*- lexical-binding: t -*-
;;; Commentary:
;;; Code:

(require 'ox)
(require 'ox-html)


(setq notes-base-dir "~/wspace/notes/")
(setq notes-html-root-dir "~/wspace/notes_site/")

(setq notes-site-html-head (concat "<link rel=\"icon\" href=\"../../static/img/favicon.png\" type=\"image/png\">\n"
                                   "<link rel=\"stylesheet\" title=\"Standard\" href=\"../../static/css/htmlize.css\" type=\"text/css\"/>\n"
                                   "<link rel=\"stylesheet\" title=\"Standard\" href=\"../../static/css/org.css\" type=\"text/css\"/>\n"))

(defun ctrlwei/org-html-template (contents info)
  "Just a copy of function org-html-template in ox-html.el'.
Return complete document string after HTML conversion.
CONTENTS is the transcoded contents string.  INFO is a plist
holding export options."
  (concat
   (org-html-doctype info)
   "\n"
   ;; (concat "<html"
   ;;         (format " lang=\"%s\"" (plist-get info :language))
   ;;         ">\n")
   "<html>\n<head>\n"
   (org-html--build-meta-info info)
   (org-html--build-head info)
   (org-html--build-mathjax-config info)
   "</head>\n"
   "<body>\n"
   (let ((link-up (org-trim (plist-get info :html-link-up)))
	 (link-home (org-trim (plist-get info :html-link-home))))
     (unless (and (string= link-up "") (string= link-home ""))
       (format (plist-get info :html-home/up-format)
	       (or link-up link-home)
	       (or link-home link-up))))
   ;; Preamble.
   (org-html--build-pre/postamble 'preamble info)
   ;; Document contents.
   (let ((div (assq 'content (plist-get info :html-divs))))
     (format "<%s id=\"%s\">\n" (nth 1 div) (nth 2 div)))
   ;; Document title.
   (when (plist-get info :with-title)
     (let ((title (and (plist-get info :with-title)
		       (plist-get info :title)))
	   (subtitle (plist-get info :subtitle))
	   (html5-fancy (org-html--html5-fancy-p info)))
       (when title
	 (format
	  "<h1 class=\"title\">%s%s</h1>\n"
	  (org-export-data title info)
	  (if subtitle
	      (format
               (concat "</h1>\n" (org-html-close-tag "br" nil info) "\n"
			 "<h1 class=\"subtitle\">%s")
	       (org-export-data subtitle info))
	    "")))))
   contents
   (format "</%s>\n" (nth 1 (assq 'content (plist-get info :html-divs))))
   ;; Postamble.
   (org-html--build-pre/postamble 'postamble info)
   ;; Possibly use the Klipse library live code blocks.
   (when (plist-get info :html-klipsify-src)
     (concat "<script>" (plist-get info :html-klipse-selection-script)
	     "</script><script src=\""
	     org-html-klipse-js
	     "\"></script><link rel=\"stylesheet\" type=\"text/css\" href=\""
	     org-html-klipse-css "\"/>"))
   ;; Closing document.
   "</body>\n</html>"))


(org-export-define-derived-backend 'ctrlwei-note-html 'html
  :translate-alist '((template . ctrlwei/org-html-template)))

(defun ctrlwei/html-publish-to-html (plist filename pub-dir)
  (org-publish-org-to 'ctrlwei-note-html filename ".html" plist pub-dir))

(defun ctrlwei/notes-sitemap-function (title list)
  "Custom site map, as a string.
TITLE is the the title of the site map.  LIST is an internal
representation for the files to include, as returned by
`org-list-to-lisp'.  PROJECT is the current project."
  (concat "#+TITLE: " title "\n"
          "\n#+OPTIONS: html-preamble:nil html-postamble:nil"
          "\n#+MACRO: span @@html:<span class=\"notes-dir-name\">$1</span>@@"
          "\n#+HTML_HEAD: <link rel=\"stylesheet\" title=\"Standard\" href=\"../static/css/index.css\" type=\"text/css\"/>"
          "\n#+HTML_HEAD_EXTRA: <link rel=\"icon\" href=\"../static/img/favicon.png\" type=\"image/png\">\n\n"
	  (org-list-to-org list)))

(defun ctrlwei/notes-publish-sitemap-default-entry (entry style project)
  "Default format for site map ENTRY, as a string.
ENTRY is a file name.  STYLE is the style of the sitemap.
PROJECT is the current project."
  (cond ((not (directory-name-p entry))
	 (format "[[file:%s][%s]]"
		 entry
		 (org-publish-find-title entry project)))
        ((directory-name-p entry)
         (format "{{{span(%s)}}}"
		 (replace-regexp-in-string "/" "" entry)))
	((eq style 'tree)
	 ;; Return only last subdir.
	 (file-name-nondirectory (directory-file-name entry)))
	(t entry)))

;; Change the path of MathJax
(setf (cdr (assq 'path org-html-mathjax-options))
      '("https://cdn.bootcss.com/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_HTMLorMML"))

(setq org-publish-project-alist
      `(("notes" :components ("notes-org" "notes-site-static" "about-note-site"))
        ("notes-org"
         :base-directory ,(expand-file-name "notes-org/" notes-base-dir)
         :base-extension "org"
         :html-extension "html"
         :publishing-directory ,(expand-file-name "notes_html/" notes-html-root-dir)
         :publishing-function (ctrlwei/html-publish-to-html)
         :recursive t
         :language "zh-CN"
         :with-latex t
         :section-numbers nil
         :html-preamble (lambda (arg) "<nav><div><a href=\"../index.html\">Home</a></div><div><a href=\"../about.html\">About</a></div></nav>")

         :auto-sitemap t
         :sitemap-filename "index.org"
         :sitemap-title "ctrlwei's notes"
         :sitemap-function ctrlwei/notes-sitemap-function
         :sitemap-format-entry ctrlwei/notes-publish-sitemap-default-entry

         :html-doctype "html5"
         :html-head-include-default-style nil
         :html-head-include-scripts nil
         :html-head ,notes-site-html-head)

        ("notes-site-static"
         :base-directory ,(expand-file-name "static/" notes-base-dir)
         :base-extension "css\\|js\\|ico\\|png\\|jpg"
         :recursive t
         :publishing-directory ,(expand-file-name "static/" notes-html-root-dir)
         :publishing-function org-publish-attachment)

        ("about-note-site"
         :base-directory ,(expand-file-name "about/" notes-base-dir)
         :base-extension "html"
         :publishing-directory ,(expand-file-name "notes_html/" notes-html-root-dir)
         :publishing-function org-publish-attachment)
        ))


(provide 'init-org-publish)
;;; init-org-publish.el ends here
